<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      python/网络编程/10-基于UDP协议的socket套接字编程 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>python/网络编程/10-基于UDP协议的socket套接字编程</h2>



  <p class="post-date">2020-10-17</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="一、UDP套接字简单示例"><a href="#一、UDP套接字简单示例" class="headerlink" title="一、UDP套接字简单示例"></a>一、UDP套接字简单示例</h1><h2 id="1-1-服务端"><a href="#1-1-服务端" class="headerlink" title="1.1 服务端"></a>1.1 服务端</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line">server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  <span class="comment"># 数据报协议-》UDP</span></span><br><span class="line">server.bind((<span class="string">'127.0.0.1'</span>, <span class="number">8080</span>))</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    data, client_addr = server.recvfrom(<span class="number">1024</span>)</span><br><span class="line">    print(<span class="string">'===&gt;'</span>, data, client_addr)</span><br><span class="line">    server.sendto(data.upper(), client_addr)</span><br><span class="line"></span><br><span class="line">server.close()</span><br></pre></td></tr></table></figure>

<h1 id="二、客户端"><a href="#二、客户端" class="headerlink" title="二、客户端"></a>二、客户端</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line">client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  <span class="comment"># 数据报协议-》UDP</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    msg = input(<span class="string">'&gt;&gt;: '</span>).strip()  <span class="comment"># msg=''</span></span><br><span class="line">    client.sendto(msg.encode(<span class="string">'utf-8'</span>), (<span class="string">'127.0.0.1'</span>, <span class="number">8080</span>))</span><br><span class="line">    data, server_addr = client.recvfrom(<span class="number">1024</span>)</span><br><span class="line">    print(data)</span><br><span class="line"></span><br><span class="line">client.close()</span><br></pre></td></tr></table></figure>

<ul>
<li>UDP是无链接的，先启动哪一端都不会报错</li>
<li>UDP协议是数据报协议，发空的时候也会自带报头，因此客户端输入空，服务端也能收到</li>
</ul>
<h1 id="三、UPD套接字无粘包问题"><a href="#三、UPD套接字无粘包问题" class="headerlink" title="三、UPD套接字无粘包问题"></a>三、UPD套接字无粘包问题</h1><h2 id="3-1-服务端"><a href="#3-1-服务端" class="headerlink" title="3.1 服务端"></a>3.1 服务端</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line">server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  <span class="comment"># 数据报协议-》udp</span></span><br><span class="line">server.bind((<span class="string">'127.0.0.1'</span>, <span class="number">8080</span>))</span><br><span class="line"></span><br><span class="line">data, client_addr = server.recvfrom(<span class="number">1024</span>)  <span class="comment"># b'hello'==&gt;b'h'</span></span><br><span class="line">print(<span class="string">'第一次：'</span>, client_addr, data)</span><br><span class="line"></span><br><span class="line">data, client_addr = server.recvfrom(<span class="number">1024</span>)  <span class="comment"># b'world' =&gt;b'world'</span></span><br><span class="line">print(<span class="string">'第二次：'</span>, client_addr, data)</span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># data,client_addr=server.recvfrom(1024)</span></span><br><span class="line"><span class="comment"># print('第三次：',client_addr,data)</span></span><br><span class="line"></span><br><span class="line">server.close()</span><br></pre></td></tr></table></figure>

<h2 id="3-2-客户端"><a href="#3-2-客户端" class="headerlink" title="3.2 客户端"></a>3.2 客户端</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line">client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  <span class="comment"># 数据报协议-》udp</span></span><br><span class="line"></span><br><span class="line">client.sendto(<span class="string">'hello'</span>.encode(<span class="string">'utf-8'</span>), (<span class="string">'127.0.0.1'</span>, <span class="number">8080</span>))</span><br><span class="line">client.sendto(<span class="string">'world'</span>.encode(<span class="string">'utf-8'</span>), (<span class="string">'127.0.0.1'</span>, <span class="number">8080</span>))</span><br><span class="line"><span class="comment"># client.sendto(''.encode('utf-8'),('127.0.0.1',8080))</span></span><br><span class="line"></span><br><span class="line">client.close()</span><br></pre></td></tr></table></figure>

<ul>
<li>UPD协议一般不用于传输大数据。</li>
<li>UDP套接字虽然没有粘包问题，但是不能替代TCP套接字，因为UPD协议有一个缺陷：如果数据发送的途中，数据丢失，则数据就丢失了，而TCP协议则不会有这种缺陷，因此一般UPD套接字用户无关紧要的数据发送，例如qq聊天。</li>
</ul>
<h1 id="四、qq聊天"><a href="#四、qq聊天" class="headerlink" title="四、qq聊天"></a>四、qq聊天</h1><ul>
<li>由于UDP无连接，所以可以同时多个客户端去跟服务端通信</li>
</ul>
<h2 id="4-1-服务端"><a href="#4-1-服务端" class="headerlink" title="4.1 服务端"></a>4.1 服务端</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#_*_coding:utf-8_*_</span></span><br><span class="line">__author__ = <span class="string">'lqz'</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line">ip_port = (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>)</span><br><span class="line">UDP_server_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  <span class="comment">#买手机</span></span><br><span class="line">UDP_server_sock.bind(ip_port)</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    qq_msg, addr = UDP_server_sock.recvfrom(<span class="number">1024</span>)</span><br><span class="line">    print(<span class="string">'来自[%s:%s]的一条消息:\033[1;44m%s\033[0m'</span> %</span><br><span class="line">          (addr[<span class="number">0</span>], addr[<span class="number">1</span>], qq_msg.decode(<span class="string">'utf-8'</span>)))</span><br><span class="line">    back_msg = input(<span class="string">'回复消息: '</span>).strip()</span><br><span class="line"></span><br><span class="line">    UDP_server_sock.sendto(back_msg.encode(<span class="string">'utf-8'</span>), addr)</span><br></pre></td></tr></table></figure>

<h2 id="4-2-客户端"><a href="#4-2-客户端" class="headerlink" title="4.2 客户端"></a>4.2 客户端</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#_*_coding:utf-8_*_</span></span><br><span class="line">__author__ = <span class="string">'lqz'</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line">BUFSIZE = <span class="number">1024</span></span><br><span class="line">UDP_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</span><br><span class="line"></span><br><span class="line">qq_name_dic = &#123;</span><br><span class="line">    <span class="string">'狗哥alex'</span>: (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>),</span><br><span class="line">    <span class="string">'瞎驴'</span>: (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>),</span><br><span class="line">    <span class="string">'一棵树'</span>: (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>),</span><br><span class="line">    <span class="string">'武大郎'</span>: (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>),</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    qq_name = input(<span class="string">'请选择聊天对象: '</span>).strip()</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        msg = input(<span class="string">'请输入消息,回车发送: '</span>).strip()</span><br><span class="line">        <span class="keyword">if</span> msg == <span class="string">'quit'</span>: <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> msg <span class="keyword">or</span> <span class="keyword">not</span> qq_name <span class="keyword">or</span> qq_name <span class="keyword">not</span> <span class="keyword">in</span> qq_name_dic: <span class="keyword">continue</span></span><br><span class="line">        UDP_client_socket.sendto(msg.encode(<span class="string">'utf-8'</span>), qq_name_dic[qq_name])</span><br><span class="line"></span><br><span class="line">        back_msg, addr = UDP_client_socket.recvfrom(BUFSIZE)</span><br><span class="line">        print(<span class="string">'来自[%s:%s]的一条消息:\033[1;44m%s\033[0m'</span> %</span><br><span class="line">              (addr[<span class="number">0</span>], addr[<span class="number">1</span>], back_msg.decode(<span class="string">'utf-8'</span>)))</span><br><span class="line"></span><br><span class="line">UDP_client_socket.close()</span><br></pre></td></tr></table></figure>

<h2 id="4-3-客户端"><a href="#4-3-客户端" class="headerlink" title="4.3 客户端"></a>4.3 客户端</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#_*_coding:utf-8_*_</span></span><br><span class="line">__author__ = <span class="string">'lqz'</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line">BUFSIZE = <span class="number">1024</span></span><br><span class="line">UDP_client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</span><br><span class="line"></span><br><span class="line">qq_name_dic = &#123;</span><br><span class="line">    <span class="string">'狗哥alex'</span>: (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>),</span><br><span class="line">    <span class="string">'瞎驴'</span>: (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>),</span><br><span class="line">    <span class="string">'一棵树'</span>: (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>),</span><br><span class="line">    <span class="string">'武大郎'</span>: (<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>),</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    qq_name = input(<span class="string">'请选择聊天对象: '</span>).strip()</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        msg = input(<span class="string">'请输入消息,回车发送: '</span>).strip()</span><br><span class="line">        <span class="keyword">if</span> msg == <span class="string">'quit'</span>: <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> msg <span class="keyword">or</span> <span class="keyword">not</span> qq_name <span class="keyword">or</span> qq_name <span class="keyword">not</span> <span class="keyword">in</span> qq_name_dic: <span class="keyword">continue</span></span><br><span class="line">        UDP_client_socket.sendto(msg.encode(<span class="string">'utf-8'</span>), qq_name_dic[qq_name])</span><br><span class="line"></span><br><span class="line">        back_msg, addr = UDP_client_socket.recvfrom(BUFSIZE)</span><br><span class="line">        print(<span class="string">'来自[%s:%s]的一条消息:\033[1;44m%s\033[0m'</span> %</span><br><span class="line">              (addr[<span class="number">0</span>], addr[<span class="number">1</span>], back_msg.decode(<span class="string">'utf-8'</span>)))</span><br><span class="line"></span><br><span class="line">UDP_client_socket.close()</span><br></pre></td></tr></table></figure>

<h2 id="4-4-运行结果"><a href="#4-4-运行结果" class="headerlink" title="4.4 运行结果"></a>4.4 运行结果</h2><ul>
<li>服务端运行结果</li>
</ul>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gjrn7nlnpmj30hn05gmyn.jpg" alt="125-基于UDP协议的套接字编程-聊天1.png?x-oss-process=style/watermark"></p>
<ul>
<li>客户端1运行结果</li>
</ul>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gjrn7tgq6sj30in06c0ud.jpg" alt="125-基于UDP协议的套接字编程-聊天2.png?x-oss-process=style/watermark"></p>
<ul>
<li>客户端2运行结果</li>
</ul>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gjrn7xkrv9j30ed04gt9o.jpg" alt="125-基于UDP协议的套接字编程-聊天3.png?x-oss-process=style/watermark"></p>
</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/python/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/9-%E8%A7%A3%E5%86%B3%E7%B2%98%E5%8C%85%E9%97%AE%E9%A2%98/">
        <span class="nav-arrow">← </span>
        
          python/网络编程/9-解决粘包问题
        
      </a>
    
    
      <a class="nav-right" href="/python/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/2-%E4%BA%92%E8%81%94%E7%BD%91%E5%92%8C%E4%BA%92%E8%81%94%E7%BD%91%E7%9A%84%E7%BB%84%E6%88%90/">
        
          python/网络编程/2-互联网和互联网的组成
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#一、UDP套接字简单示例"><span class="toc-nav-text">一、UDP套接字简单示例</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-1-服务端"><span class="toc-nav-text">1.1 服务端</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#二、客户端"><span class="toc-nav-text">二、客户端</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#三、UPD套接字无粘包问题"><span class="toc-nav-text">三、UPD套接字无粘包问题</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-1-服务端"><span class="toc-nav-text">3.1 服务端</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-2-客户端"><span class="toc-nav-text">3.2 客户端</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#四、qq聊天"><span class="toc-nav-text">四、qq聊天</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-1-服务端"><span class="toc-nav-text">4.1 服务端</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-2-客户端"><span class="toc-nav-text">4.2 客户端</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-3-客户端"><span class="toc-nav-text">4.3 客户端</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-4-运行结果"><span class="toc-nav-text">4.4 运行结果</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/python/网络编程/10-基于UDP协议的socket套接字编程/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "python/网络编程/10-基于UDP协议的socket套接字编程",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>